//
//  ZKPickerView.m
//  Homept_ios
//
//  Created by pro on 16/9/26.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "ZKPickerView.h"

@implementation ZKPickerView
{
    // data
    NSMutableArray  *_selectedIndexAry;
}

#pragma mark - 视图初始化
- (void)setupUI {

    self.contentMode = ByPickerContentModeBottom;
    
    _arrayData = @[];
    
    [self.pickerView setDelegate:self];

    [self.pickerView setDataSource:self];
}


#pragma mark - setters
- (void)setArrayData:(NSArray *)arrayData {
    
    _arrayData          = arrayData;
    _selectedIndexAry   = [NSMutableArray new];
    for (int i = 0; i < _arrayData.count; i ++) {
        [_selectedIndexAry addObject:@(0)];
    }
    [self.pickerView reloadAllComponents];
}
- (void)setArratData:(NSArray *)arrayData andInitRows:(NSArray *)rows {
    _arrayData          = arrayData;
    _selectedIndexAry   = [NSMutableArray new];
    for (int i = 0; i < _arrayData.count; i ++) {
        [_selectedIndexAry addObject:@(0)];
    }
    [self.pickerView reloadAllComponents];
    for (int i = 0; i < rows.count; i ++) {
        NSArray *temp = _arrayData[i];
        NSInteger selectRow = [temp indexOfObject:rows[i]];
        [self.pickerView selectRow:selectRow inComponent:i animated:NO];
        [_selectedIndexAry replaceObjectAtIndex:i withObject:@(selectRow)];
    }

}

#pragma mark - picker delegate
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return _arrayData.count;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    NSArray *temp = _arrayData[component];
    return temp.count;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 44;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    
    return self.width / _arrayData.count;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    
    [_selectedIndexAry replaceObjectAtIndex:component withObject:@(row)];
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view {
    
    UILabel *label = [[UILabel alloc] init];
    [label setTextAlignment:NSTextAlignmentCenter];
    
    NSArray *temp = _arrayData[component];
    label.text = temp[row];
   
    return label;
}

#pragma mark - 点击
- (void)selectedOk {
    
    if (self.pickerDidSelected) {
        NSMutableArray *textAry = [[NSMutableArray alloc] initWithCapacity:_selectedIndexAry.count];
        for (int i = 0; i < _selectedIndexAry.count; i ++) {
            NSArray *temp = _arrayData[i];
            NSInteger selectedIndex = [_selectedIndexAry[i] integerValue];
            [textAry addObject:temp[selectedIndex]];
        }
        self.pickerDidSelected([textAry copy]);
        
    }
    
    [super selectedOk];
}


@end
